﻿using System.Windows;
using System.Windows.Controls;

namespace ProjTemp.Desktop.Common.Controls
{
    /// <summary>
    /// Interaction logic for BusyIndicator.xaml
    /// </summary>
    public class BusyIndicator : ContentControl
    {
        public static readonly DependencyProperty IsBusyProperty = DependencyProperty.Register("IsBusy", typeof(bool), typeof(BusyIndicator), new PropertyMetadata(false, new PropertyChangedCallback(OnIsBusyChanged)));
        public static readonly DependencyProperty BusyMessageProperty = DependencyProperty.Register("BusyMessage", typeof(string), typeof(BusyIndicator));
        public static readonly DependencyProperty IndicatorTypeProperty = DependencyProperty.Register("IndicatorType", typeof(BusyIndicatorType), typeof(BusyIndicator), new PropertyMetadata(BusyIndicatorType.Line));
        public static readonly DependencyProperty ShowProgressProperty = DependencyProperty.Register("ShowProgress", typeof(bool), typeof(BusyIndicator), new PropertyMetadata(false));
        public static readonly DependencyProperty ProgressProperty = DependencyProperty.Register("Progress", typeof(double), typeof(BusyIndicator), new PropertyMetadata(0.0));

        public BusyIndicator()
        {
        }

        /// <summary>
        /// Is busy or not
        /// </summary>
        public bool IsBusy
        {
            get { return (bool)this.GetValue(IsBusyProperty); }
            set { this.SetValue(IsBusyProperty, value); }
        }

        /// <summary>
        /// Busy message content
        /// </summary>
        public string BusyMessage
        {
            get { return (string)this.GetValue(BusyMessageProperty); }
            set { this.SetValue(BusyMessageProperty, value); }
        }

        /// <summary>
        /// Busy indicator type
        /// </summary>
        public BusyIndicatorType IndicatorType
        {
            get { return (BusyIndicatorType)this.GetValue(IndicatorTypeProperty); }
            set { this.SetValue(IndicatorTypeProperty, value); }
        }

        /// <summary>
        /// Show progress or not
        /// </summary>
        public bool ShowProgress
        {
            get { return (bool)this.GetValue(ShowProgressProperty); }
            set { this.SetValue(ShowProgressProperty, value); }
        }

        /// <summary>
        /// Progress data
        /// </summary>
        public double Progress
        {
            get { return (double)this.GetValue(ProgressProperty); }
            set { this.SetValue(ProgressProperty, value); }
        }

        private static void OnIsBusyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var busyIndicator = sender as BusyIndicator;
            if (busyIndicator != null)
                VisualStateManager.GoToState(busyIndicator, (bool)e.NewValue ? "Busy" : "Idle", true);                
        }
    }

    /// <summary>
    /// Busy indicator type
    /// </summary>
    public enum BusyIndicatorType
    {
        /// <summary>
        /// use ProgressRing
        /// </summary>
        Ring = 1,
        /// <summary>
        /// use ProgressIndicator
        /// </summary>
        Line,
        /// <summary>
        /// use ProgressBar
        /// </summary>
        Bar
    }
}